package com.example.yuejianapp14.Activity.Adapter;

import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejianapp14.Activity.Activity.UserCollectedActivity;
import com.example.yuejianapp14.Activity.JavaBean.Collected;
import com.example.yuejianapp14.Activity.JavaBean.Success;
import com.example.yuejianapp14.Activity.Listener.ListListener;
import com.example.yuejianapp14.Activity.Model.CollectedModel;
import com.example.yuejianapp14.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by jason_lin on 2017/11/9.
 */

public class UserCollectedAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private UserCollectedActivity mContext;
    private List<Collected> mDataList;
    private LayoutInflater mLayoutInflater;
    private SharedPreferences sp;

    public UserCollectedAdapter(UserCollectedActivity mContext, List mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v =mLayoutInflater.from(parent.getContext()).inflate(R.layout.collectedcard,parent,false);
        return new ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Collected entity = (Collected) mDataList.get(position);
        if (null == entity)
            return;
        ViewHolder viewHolder = (ViewHolder) holder;
        if (entity.getClubid()!=null){
            viewHolder.collected_name.setText(entity.getClubname());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/club/"+entity.getClubpic()).into(viewHolder.collected_pic);
        }
        if (entity.getCoachid()!=null){
            viewHolder.collected_name.setText(entity.getCoachname());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/coachphoto/"+entity.getCoachpic()).into(viewHolder.collected_pic);
        }
        if (entity.getActivityid()!=null){
            viewHolder.collected_name.setText(entity.getActivity());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/activity/"+entity.getActivitypic()).into(viewHolder.collected_pic);
        }
        if (entity.getClassid()!=null){
            viewHolder.collected_name.setText(entity.getClassname());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/class/"+entity.getClasspic()).into(viewHolder.collected_pic);
        }
        if (entity.getCompetitionid()!=null){
            viewHolder.collected_name.setText(entity.getCompetition());
            Picasso.with(mContext).load("http://10.0.2.2/YueJian/Public/photo/competition/"+entity.getPic()).into(viewHolder.collected_pic);
        }
        viewHolder.cancelcollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    CollectedModel collectedModel = new CollectedModel();
                    collectedModel.Collect(sp.getString("uid",""),entity.getType(),entity.getTypeid(),collectListener);
            }
        });
    }
    ListListener<Success> collectListener=new ListListener<Success>() {
        @Override
        public void onResponse(Success a) {
                 if (a.getSuccess().equals("0")){
                Toast.makeText(mContext, "成功取消关注",Toast.LENGTH_SHORT).show();
                mContext.getData();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(mContext, "fail",Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        ImageView collected_pic;
        TextView collected_name;
        LinearLayout collected_container;
        ImageButton cancelcollect;

        public ViewHolder(View itemView) {
            super(itemView);
            collected_name = (TextView) itemView.findViewById(R.id.collected_name);
            collected_pic = (ImageView) itemView.findViewById(R.id.collected_pic);
            collected_container = (LinearLayout) itemView.findViewById(R.id.collected_container);
            cancelcollect =(ImageButton)itemView.findViewById(R.id.cancelcollect);
            sp=mContext.getSharedPreferences("uid",MODE_PRIVATE);
        }
    }
}
